clear
set mem 800m
set more off
capture log close

*do ${fmartorell_home}top_program

*log using ${fmartorell_home}programs/newreport2.log, replace
log using ${d1}log/newreport2.log, replace

*global rawpath "${receipts}/thecb/thecb_07-04/"
* note: this folder is from ${receipts}/thecb/thecb_07-01/
global rawpath "${d1}thecb/thecb_07-01/"

*global data "${fmartorell_home}data"
global data "${d1}data"

program drop _all
program define readin
clear
label drop _all
args sem gcyr fileyr

# delimit ;
infix byte rectype	1
      long school	   2-7 
      str9 altpid	   8-16 
      byte semester	17 
      int  year	   18-21 
      byte  nondisc	   22
      str1 _sex		23 
      byte _ethnic	24 
      int  byr		   25-28
      int  bmo		   29-30 
      int  bda		   31-32
      int  sch		   33-35 
      int  gpa		   36-38 
      int  mathdsch  39-40     /* NOTE: the dev ed credit hours appear to be NFG!!!! */
      int  writdsch 	41-42 
      int  readdsch  43-44
      
      /* Math TSI variables */
      long math_prev_school  45-50
      int  math_stsfc_year   51-54
      byte math_stsfc_term   55
      byte mathstat_tsi      56
      int  math_exwv_year    57-60
      byte math_exwv_term    61
      str1 math_exwv_status  62
      int  math_init_score   63-65
      byte math_init_pass    66
      int  math_init_year    67-70
      byte math_init_month   71-72
      byte math_init_day     73-74
      byte math_init_type    75
      int math_subs_high     76-78
      byte math_subs_pass    79 
      int  math_subs_year    80-83
      byte math_subs_month   84-85
      byte math_subs_day     86-87
      byte math_subs_type    88
      int  math_course_year  89-92
      byte math_course_term  93
      byte math_course_grade 94

      /* Writing TSI variables */
      long writ_prev_school  95-100
      int  writ_stsfc_year   101-104
      byte writ_stsfc_term   105   
      byte writstat_tsi      106   
      int  writ_exwv_year    107-110
      byte writ_exwv_term    111   
      str1 writ_exwv_status  112   
      int  writ_init_score   113-115
      byte writ_init_pass    116   
      int  writ_init_year    117-120
      byte writ_init_month   121-122
      byte writ_init_day     123-124
      byte writ_init_type    125   
      int writ_subs_high     126-128
      byte writ_subs_pass    129   
      int  writ_subs_year    130-133
      byte writ_subs_month   134-135
      byte writ_subs_day     136-137
      byte writ_subs_type    138   
      int  writ_course_year  139-142
      byte writ_course_term  143   
      byte writ_course_grade 144   

      /* Reading TSI variables */
      long read_prev_school  145-150
      int  read_stsfc_year   151-154
      byte read_stsfc_term   155   
      byte readstat_tsi      156   
      int  read_exwv_year    157-160
      byte read_exwv_term    161   
      str1 read_exwv_status  162   
      int  read_init_score   163-165
      byte read_init_pass    166   
      int  read_init_year    167-170
      byte read_init_month   171-172
      byte read_init_day     173-174
      byte read_init_type    175   
      int read_subs_high     176-178
      byte read_subs_pass    179   
      int  read_subs_year    180-183
      byte read_subs_month   184-185
      byte read_subs_day     186-187
      byte read_subs_type    188   
      int  read_course_year  189-192
      byte read_course_term  193   
      byte read_course_grade 194   

      /* byte ccenroll	28 
      str1 update	   29 
      byte firstsem	30 
      int  fyear	   31-34
      byte objectiv	35
      str9 pstudpid	36-44 
      byte unused1	55
      byte teststat	56
      byte inittest	57
      byte mathstat	58
      byte mathobli	59
      byte mdevstat	60
      byte mathlevl	63
      byte mathprov	64
      int  initmath	65-67
      byte mathpass	68
      int  mthscor1  69-71
      int  mthscor2  72-74 
      byte gradmath	75
      byte writstat  76 
      byte writobli	77 
      byte wdevstat	78 
      byte writlevl	81 
      byte writprov	82 
      int  initwrit  83-85 
      byte writpass	86 
      int  wrtscor1	87-89 
      int  wrtscor2	90-92 
      byte gradwrit  93 
      byte readstat  94 
      byte readobli  95 
      byte rdevstat  96 
      byte readlevl  99 
      byte readprov  100 
      int  initread  101-103 
      byte readpass  104 
      int  redscor1	105-107 
      int  redscor2	108-110 
      byte gradread	111
      str9 junk		112-120 */
      using "${rawpath}d2sr`sem'0`fileyr'.", clear;

  display in white "Reading file: ${path}D1sr`sem'`fileyr'.t";

  display in white "Dates--incl. setting bday of 0 to 15:" _continue; generate bdate = mdy(bmo,bda,byr);
  format bdate %dN/D/Y;
  drop byr bmo bda;

  generate cur_year=`gcyr';
  #delimit cr
  
  display in white "Check years"
  tabulate cur_year semester
  tabulate year semester


  display in white "--- Sex ---"
  generate byte sex = 0 if _sex == "F"
  replace sex = 1 if _sex == "M"
  label define sex 0 "female" 1 "male"
  label values sex sex
  drop _sex

  display in white "--- Ethnic ---"
  generate ethnic=.
  quietly for num 1/7 \ num 5 4 2 3 1 6 7: replace ethnic=X if _ethnic==Y
  label define ethnic 1 "NAT.AM" 2 "ASIAN" 3 "AFR.AM" 4 "HISPANIC" 5 "ANGLO" 6 "FOREIGN" 7 "UNKNOWN"
  label values ethnic ethnic


  label var rectype  "Record Code (always 2)"
  label var school   "College"
  label var altpid   "EncryptedSSN"
  label var sex      "Gender"
  label var bdate    "Birthdate"
  label var ethnic   "Race / Ethnicity"
  label var semester "Current Semester"
  label var year     "Calendar Year"
  label var cur_year "TSP year"
  bysort altpid school: assert _N==1
  drop _ethnic 
  quietly compress
  gen testgpa=gpa/sch
  summ testgpa
  save ${d1}data/d2sr`gcyr'_`sem',replace

end


*readin 1 204 3
*readin 2 204 4
readin 3 204 4
readin 1 205 4
readin 2 205 5
readin 3 205 5

log close

